/**
 * Created by xiangzhihong on 2017/5/13.
 */
import moment from 'moment'
/**
 * 根据日期获取星期
 * @param date
 * @returns {*}
 */
function getWeekdayByDate(date) {
    let a = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
    let day = new Date(date).getDay();
    var today = getNowFormatDate();
    var tomorrow = getTomorrowFormatDate();
    if (today === date)
        return '今天';
    else if (date === tomorrow)
        return '明天'
    else
        return a[day];
}

/**
 * 获取今天的时间戳
 * @returns {string}
 */
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    return currentdate;
}
//获取当前的时间戳
function getCurrentTimeStamp() {
    return moment().toDate().getTime();
}
//返回指定时间的时间戳
function getTimeStamp(timeStr) {
    return moment(timeStr).toDate().getTime();
}

/**
 * 获取明天的时间戳
 * @returns {string}
 */
function getTomorrowFormatDate() {
    var date = new Date();
    date.setTime(date.getTime() + 24 * 60 * 60 * 1000);
    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    return currentdate;
}

/**
 * 根据时间戳获取月日
 * @param date
 */
function getMonthAndDayByDate(date) {
    return date.substring(8, date.length) + '日';
}

/**
 * 根据时间戳获取时分
 * @param date
 */
function getHoursAndMinsByDate(date) {
    return date.substring(11, date.length);
}

/**
 * 根据时间戳获取和格式获取对应的日期
 * @param date
 */
function formatDate(timestamp, formater) {
    let date = new Date();
    date.setTime(parseInt(timestamp));
    formater = (formater != null) ? formater : 'yyyy-MM-dd';
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月
            "d+": this.getDate() + 1, //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };

        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ?
                (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
        return fmt;
    }
    return date.Format(formater);
}


module.exports = {
    formatDate: formatDate,
    getNowFormatDate: getNowFormatDate,
    getTomorrowFormatDate: getTomorrowFormatDate,
    getWeekdayByDate: getWeekdayByDate,
    getMonthAndDayByDate: getMonthAndDayByDate,
    getHoursAndMinsByDate: getHoursAndMinsByDate,
    getCurrentTimeStamp:getCurrentTimeStamp,
    getTimeStamp:getTimeStamp,
}